package com.wwwarehouse.usercenter.fragment.share;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.share.ShareContent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.response.InviteInfoResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;
import com.wwwarehouse.usercenter.fragment.invite.InviteDetailsFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * author ：minifly
 * date: 2017/8/31
 * time: 14:35
 *
 */

@Route(path = UserCenterConstant.PATH_SHARE_INVITED_DETAIL_OWN)
public class ShareInviteDetailsFragment extends InviteDetailsFragment {

    private Dialog mDialog;
    private BaseApplication application;
    private ShareContent content;
    private int currentSendType;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        application = BaseApplication.getApplicationInstance();

        Bundle bundle = getArguments();
        if(bundle.getParcelable("content") != null ){
            content = bundle.getParcelable("content");
        }

    }

    @Override
    public void adapterListener(final List<InviteInfoResponseBean.InviteDetailsBean> inviteDetailList) {
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mInviteDetailsBean = inviteDetailList.get(position);

                if (mInviteDetailsBean.getInviteeStatus().getInviteeStatusCode() == 0) {
                } else {
                }

                showCustomDialogPrompt(getContext(), TYPE_TEXT, new ShareBtnListener() {
                    @Override
                    public void cancle() {

                    }
                    @Override
                    public void confirm() {
                        sendInfo(mInviteDetailsBean);
                    }
                });
            }
        });
    }

    /**
     * 带取消确定的dialog
     *
     * @param title         标题
     * @param content       提示内容
     */
    private final static int TYPE_IMAGE = 10001, TYPE_TEXT = 10002, TYPE_ALL = 10003, TYPE_ALLNOTITLE = 10004;

    public void showCustomDialogPrompt(Context context, int type, final ShareBtnListener listener) {

        Button btnCancel = null, btnConfirm = null;
        View view = null;
        switch (type) {
            case TYPE_ALL://所有数据都有 type3
                currentSendType = 3;
                view = View.inflate(context, R.layout.share_bottom_dialog_alltype, null);

                ImageView mShareAlltypeHeadimageId = (ImageView) view.findViewById(R.id.share_alltype_headimage_id);
                ImageView shareAllImage = (ImageView) view.findViewById(R.id.share_alltype_image_id);
                TextView mShareAlltypeHeadnameId = (TextView) view.findViewById(R.id.share_alltype_headname_id);
                TextView title = (TextView) view.findViewById(R.id.share_alltype_title_id);
                TextView contentTxt = (TextView) view.findViewById(R.id.share_alltype_content_id);
                btnCancel = (Button) view.findViewById(R.id.share_alltype_btn_cancel);
                btnConfirm = (Button) view.findViewById(R.id.share_alltype_btn_confirm);

                application.displayImg(content.getImageUrl(),shareAllImage);
                application.displayImg(mInviteDetailsBean.getFaceUrl(),mShareAlltypeHeadimageId);

                title.setText(content.getTitle());
                contentTxt.setText(content.getContent());
                mShareAlltypeHeadnameId.setText(content.getShareDestname());


                break;
            case TYPE_ALLNOTITLE: //无title类型  type 2
                currentSendType = 2;
                view = View.inflate(context, R.layout.share_bottom_dialog_alltypenotitle, null);
                ImageView mShareAlltypenotitleHeadimageId = (ImageView) view.findViewById(R.id.share_alltypenotitle_headimage_id);
                TextView mShareAlltypenotitleHeadnameId = (TextView) view.findViewById(R.id.share_alltypenotitle_headname_id);
                ImageView mShareAlltypenotitleImageId = (ImageView) view.findViewById(R.id.share_alltypenotitle_image_id);
                TextView mShareAlltypenotitleContentId = (TextView) view.findViewById(R.id.share_alltypenotitle_content_id);
                btnCancel = (Button) view.findViewById(R.id.share_alltypenotitle_btn_cancel);
                btnConfirm = (Button) view.findViewById(R.id.share_alltypenotitle_btn_confirm);

                application.displayImg(content.getImageUrl(),mShareAlltypenotitleImageId);
                application.displayImg(mInviteDetailsBean.getFaceUrl(),mShareAlltypenotitleHeadimageId);
                mShareAlltypenotitleHeadnameId.setText(content.getShareDestname());
                mShareAlltypenotitleContentId.setText(content.getContent());

                break;
            case TYPE_IMAGE: //只有图片 type 4
                currentSendType = 4;
                view = View.inflate(context, R.layout.share_bottom_dialog_image, null);

                ImageView mShareImageHeadimageId = (ImageView) view.findViewById(R.id.share_image_headimage_id);
                TextView mShareImageHeadnameId = (TextView) view.findViewById(R.id.share_image_headname_id);
                ImageView mShareImageImageId = (ImageView) view.findViewById(R.id.share_image_image_id);
                btnCancel = (Button) view.findViewById(R.id.share_image_btn_cancel);
                btnConfirm = (Button) view.findViewById(R.id.share_image_btn_confirm);

                application.displayImg(content.getImageUrl(),mShareImageImageId);
                application.displayImg(mInviteDetailsBean.getFaceUrl(),mShareImageHeadimageId);
                mShareImageHeadnameId.setText(content.getShareDestname());

                break;
            case TYPE_TEXT: //只有文字+链接 type 1
                currentSendType = 1;
                view = View.inflate(context, R.layout.share_bottom_dialog_text, null);

                ImageView mShareTextHeadimageId = (ImageView) view.findViewById(R.id.share_text_headimage_id);
                TextView mShareTextHeadnameId = (TextView) view.findViewById(R.id.share_text_headname_id);
                TextView mShareTextTextId = (TextView) view.findViewById(R.id.share_text_text_id);

                btnCancel = (Button) view.findViewById(R.id.share_text_btn_cancel);
                btnConfirm = (Button) view.findViewById(R.id.share_text_btn_confirm);

                application.displayImg(mInviteDetailsBean.getFaceUrl(),mShareTextHeadimageId);

                mShareTextHeadnameId.setText(content.getShareDestname());
                mShareTextTextId.setText(content.getContent());

                break;
        }


        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(false);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.cancle();
                }
                mDialog.dismiss();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.confirm();
                }
                //取消掉
                mDialog.dismiss();
            }
        });
    }

    public interface ShareBtnListener{
        void cancle();
        void confirm();
    }

    private final int SHARE_SEND = 1001;

    private void sendInfo(InviteInfoResponseBean.InviteDetailsBean bean) {
        showProgressDialog(getString(R.string.waiting));
        Map map = new HashMap();
        /**
         * eceiveUserId	接收人ID	number	必填
         smallImgUrl	缩略图URL	string	@mock=http://saddadsadsadsadsad
         srcUrl	资源URL	string	必填
         summary	简介	string	@mock=我是简介
         title	标题	string	@mock=我是标题
         type	分享内容类型	number	type=1:summary:简介 type=2:summary：简介 ,smallImgUrl:小图片url type=3:summary:简介 smallImgUrl:小图片url ，title:标题 type=4，srcUrl
         */
        map.put("receiveUserId", "" + bean.getInviteeId());
        map.put("smallImgUrl", content.getImageUrl());
        map.put("srcUrl", content.getUrl());
        map.put("summary", content.getContent());
        map.put("title", content.getTitle());
        map.put("type", currentSendType);

        NoHttpUtils.httpPost(UserCenterConstant.SHARE_CONTACT_URL, map, mOnResponseListener, SHARE_SEND);
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case SHARE_SEND:
                    if ("0".equals(commonClass.getCode())) {
                        ((BaseCardDeskActivity)getContext()).popFragment();
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
        }
        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };

}
